package com.axzhengxin.platform.sync.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.ds.DSFactory;
import com.axzhengxin.platform.business.domain.BizApproveDetail;
import com.axzhengxin.platform.business.domain.BizApproveInfo;
import com.axzhengxin.platform.business.mapper.BizApproveDetailMapper;
import com.axzhengxin.platform.business.mapper.BizApproveInfoMapper;
import com.axzhengxin.platform.sync.ISyncDataService;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * BizApproveInfoSyncServiceImpl
 *
 * @author jiangnan
 * @date 2021/4/6
 */
@Slf4j
@Service
public class BizApproveInfoSyncServiceImpl implements ISyncDataService {

    /**
     * 加载所有内部流转的授信信息
     */
    private static final String LOAD_BIZ_APPROVE_INFO_SQL = "SELECT t.id AS approve_id, t.TRANSSERIAL_NO AS apply_id, t.CORNO AS customer_id, t.ENT_NAME AS customer_name, CASE  t.IS_FIRST_LOAN  WHEN '0' THEN '1'  ELSE '0' END AS IS_FIRST_approve, t.PAYMENT_DAY AS approve_start, t.PAYMENT_DUE_DAY AS approve_end, t.GHJG AS create_by, to_date(substr(t.id, 0, 8), 'yyyymmdd') AS create_time, t.BRANCH, t.CREDIT_AMOUNT AS approve_amount FROM WARNING_UPLOAD t WHERE substr(t.id, 0, 6) IN ('202010', '202011', '202012', '202101', '202102', '202103', '202104')AND t.branch IS NOT NULL AND t.TRANSSERIAL_NO IS NOT NULL";
    /**
     * 加载授信分项详情信息
     */
    private static final String LOAD_BIZ_APPROVE_DETAIL_SQL = "SELECT t.id AS approve_detail_id, t.id AS approve_id, 0 as detail_sort, t.CREDIT_AMOUNT AS approve_amount, to_date(substr(t.id, 0, 8), 'yyyymmdd') AS create_time, t.LOAN_TYPE as guarantee_type FROM WARNING_UPLOAD t WHERE substr(t.id, 0, 6) IN ('202010', '202011', '202012', '202101', '202102', '202103', '202104') AND t.branch IS NOT NULL AND t.TRANSSERIAL_NO IS NOT NULL";
    /**
     * 担保方式映射
     */
    private static final Map<String, String> GUARANTEE_TYPE_MAPPER = MapUtil.<String, String>builder()
            .put("抵押贷款", "0")
            .put("信用贷款", "2")
            .put("担保贷款", "3")
            .put("质押贷款", "1")
            .put("票据贴现", "2")
            .build();
    static int ORDER = BizApplyInfoSyncServiceImpl.ORDER + 10;
    private static Map<Long, Long> deptAdminMapper;
    @Autowired
    private BizApproveInfoMapper bizApproveInfoMapper;
    @Autowired
    private BizApproveDetailMapper bizApproveDetailMapper;

    @SneakyThrows
    @Override
    public void exec() {
        preExec();
        String profile = SpringUtils.getActiveProfile();
        Db.use(DSFactory.get(profile))
                .query(LOAD_BIZ_APPROVE_INFO_SQL)
                .stream()
                .peek(entity -> {
                    if (StrUtil.isBlank(entity.getStr("CREATE_BY"))) {
                        entity.set("CREATE_BY", deptAdminMapper.get(entity.getLong("BRANCH")));
                    }
                    entity.remove("BRANCH");
                })
                .map(entity -> entity.toBean(BizApproveInfo.class))
                .forEach(approve -> bizApproveInfoMapper.insertBizApproveInfo(approve));
        Db.use(DSFactory.get(profile))
                .query(LOAD_BIZ_APPROVE_DETAIL_SQL)
                .stream()
                .map(entity -> entity.toBean(BizApproveDetail.class))
                .peek(detail -> {
                    if (StrUtil.isBlank(detail.getGuaranteeType())) {
                        detail.setGuaranteeType(GUARANTEE_TYPE_MAPPER.get("信用贷款"));
                    } else if (!NumberUtil.isNumber(detail.getGuaranteeType())) {
                        detail.setGuaranteeType(GUARANTEE_TYPE_MAPPER.getOrDefault(detail.getGuaranteeType(), "2"));
                    }
                })
                .forEach(detail -> bizApproveDetailMapper.insertBizApproveDetail(detail));
    }

    @Override
    public int getOrder() {
        return ORDER;
    }

    @SneakyThrows
    private void preExec() {
        deptAdminMapper = loadDeptAdminMapper();
    }

}
